﻿using System;
using System.Windows.Input;
using Startup.Base.Delegates;

namespace Startup.Base.Commands
{
	public class SimpleCommand<T> : ICommand
	{
		private readonly ExecuteCommandDelegate<T> _executeAction;
		private readonly CanExecuteCommandDelegate<T> _canExecuteAction;

		public event EventHandler CanExecuteChanged;

		public SimpleCommand(ExecuteCommandDelegate<T> executeAction)
			: this(executeAction, x => true)
		{}

		public SimpleCommand(ExecuteCommandDelegate<T> executeAction, CanExecuteCommandDelegate<T> canExecuteAction)
		{
			_executeAction = executeAction;
			_canExecuteAction = canExecuteAction;
		}

		public void Execute(object parameter)
		{
			_executeAction((T)parameter);
		}

		public bool CanExecute(object parameter)
		{
			return _canExecuteAction((T)parameter);
		}

		public void RaiseCanExecuteChanged()
		{
			if (CanExecuteChanged != null)
				CanExecuteChanged(this, EventArgs.Empty);
		}
	}

	public class SimpleCommand : SimpleCommand<object>
	{
		public SimpleCommand(UnparameterizedDelegate executeAction)
			: base(x => executeAction())
		{ }

		public SimpleCommand(UnparameterizedDelegate executeAction, CanExecuteCommandDelegate canExecuteAction)
			: base(x => executeAction(), x => canExecuteAction())
		{ }
	}
}
